//
//  ViewController.m
//  图片轮播
//
//  Created by 伍银荣 on 2021/6/2.
//  Copyright © 2021 com.example. All rights reserved.
//

#import "ViewController.h"

#define imageCount 5
#define Width [UIScreen mainScreen].bounds.size.width
#define Height [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UIScrollViewDelegate>
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIPageControl *pageControl;

@property (nonatomic, strong) NSTimer *timer;
@property (strong, nonatomic) IBOutlet UIScrollView *bigScrollView;
@property (strong, nonatomic) IBOutlet UIImageView *lastimgView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bigScrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.lastimgView.frame));
    
    self.bigScrollView.contentOffset = CGPointMake(0, -98);
    
    self.bigScrollView.contentInset = UIEdgeInsetsMake(98, 0, 10, 0);
    
    CGFloat imgW = self.scrollView.frame.size.width;
    CGFloat imgH = self.scrollView.frame.size.height;
    for (int i = 0; i < imageCount; i++) {
        UIImageView *imgView = [[UIImageView alloc]init];
        CGFloat imgX = imgW * i;
        imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image%02d",i + 1]];
        imgView.frame = CGRectMake(imgX, 0, imgW, imgH);
        [self.scrollView addSubview:imgView];
    }
    self.scrollView.contentSize = CGSizeMake(imgW * imageCount, 0);
    //分页效果
    self.scrollView.pagingEnabled = YES;
    //隐藏水平滚动指示器
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    self.scrollView.delegate = self;
    
    self.pageControl.numberOfPages = imageCount;
    self.pageControl.currentPage = 0;
    
    //计时器控件
    [self createTimer];
    
    
    
    
    //这个方法需要手动开启计时器(调用一次[time fire];方法就只滚动一次)
//    NSTimer *time = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(clicked) userInfo:nil repeats:YES];
//    [time fire];
//    [time fire];

   

    //NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeClicked) userInfo:nil repeats:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"正在滚动");
    CGFloat offset = scrollView.contentOffset.x + (self.scrollView.frame.size.width * 0.5);
    int page = offset / scrollView.frame.size.width;
    self.pageControl.currentPage = page;
}

//- (void)clicked {
//    NSLog(@"滚动");
//}

//创建计时器
- (void)createTimer {
    // 计时器控件
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
        //实现每隔1秒自动滚动图片
        //获取当前UIpageControl的页码
        NSInteger page = self.pageControl.currentPage;
        if (page == self.pageControl.numberOfPages - 1) {
            page = 0;
        } else {
            page++;
        }
        CGFloat offset = self.scrollView.frame.size.width *page;
        [self.scrollView setContentOffset:CGPointMake(offset, 0) animated:NO];
    }];
    //设置计时器的优先级和常见控件的优先级一样高
    //获取当前的消息循环对象
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    //改变timer对象的优先级
    [runLoop addTimer:_timer forMode: NSRunLoopCommonModes];
    
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"开始拖拽");
    //将计时器销毁
    [self.timer invalidate];
    self.timer = nil;
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    NSLog(@"结束拖拽");
    //重新开启一个计时器
    [self createTimer];
}



@end
